# payroll pipeline makefile
TASK_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))

include ../../config.mk


# Scripts
WAGE_GROWTH_PLOT := $(TASK_DIR)/src/01_wage_growth_plot.R
PAYROLL_AVGS := $(TASK_DIR)/src/02_payroll_20mo_avgs.R
STIPEND_AVGS := $(TASK_DIR)/src/03_get_stipend_earnings.R
BUILD_EARNINGS_TABLE := $(TASK_DIR)/src/04_build_earnings_table.R
EARNINGS_PERCENTILES := $(TASK_DIR)/src/05_get_earning_percentiles.R
AVAILABLE_HOURS := $(TASK_DIR)/src/06_get_available_hours.R
RMD_FILE := $(TASK_DIR)/src/print_payroll_tables.Rmd

# Inputs (that have not already been specified in config.mk)
CONFIG_FILE := $(TASK_DIR)/hand/config.yaml

STIPEND_PRE_STACK := /projects/2016-027-readi/quant/spaghetti/temporary/stipend_stack.csv
STIPEND_COVID_STACK := /projects/2016-027-readi/quant/spaghetti/temporary/pandemic_stipend_stack.csv

# Outputs (intermediary files and final figures)
OUT_WAGE_GROWTH_PLOT := $(OUT_DIR)/wage_growth_pathway_up_to_date.png
OUT_PAYROLL_AVGS := $(OUT_DIR)/payroll_taker_avgs.csv
OUT_STIPEND_AVGS := $(OUT_DIR)/stipend_averages.csv
OUT_WAGE_TABLE := $(OUT_DIR)/combined_wage_table.csv
OUT_WAGE_PERCENTILES := $(OUT_DIR)/wage_percentiles.csv
OUT_AVAILABLE_HOURS := $(OUT_DIR)/available_hours_table.csv
OUT_MAIN_TABLE := $(OUT_DIR)/wage_table_up_to_date.tex
OUT_APPENDIX_TABLE := $(OUT_DIR)/wage_table_appendix_up_to_date.tex
OUT_PCT_HOURS_TABLE := $(OUT_DIR)/pct_hours_up_to_date.tex

# Recipes

$(OUT_WAGE_GROWTH_PLOT): $(WAGE_GROWTH_PLOT) $(CONFIG_FILE) $(PROJECT_CONFIG) $(TASK_FNS) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE)
	@mkdir -p $(OUT_DIR)
	@mkdir -p $(PAPER_OUT_DIR)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--OUT_WAGE_GROWTH_PLOT=$(OUT_WAGE_GROWTH_PLOT)
	@cp $(OUT_DIR)/*up_to_date.png $(PAPER_OUT_DIR)

$(OUT_PAYROLL_AVGS): $(PAYROLL_AVGS) $(CONFIG_FILE) $(PROJECT_CONFIG) $(TASK_FNS) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--OUT_PAYROLL_AVGS=$(OUT_PAYROLL_AVGS)

$(OUT_STIPEND_AVGS): $(STIPEND_AVGS) $(CONFIG_FILE) $(PROJECT_CONFIG) $(TASK_FNS) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE) $(STIPEND_PRE_STACK) $(STIPEND_COVID_STACK)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--STIPEND_PRE_STACK=$(STIPEND_PRE_STACK) \
		--STIPEND_COVID_STACK=$(STIPEND_COVID_STACK) \
		--OUT_STIPEND_AVGS=$(OUT_STIPEND_AVGS)

$(OUT_WAGE_TABLE): $(BUILD_EARNINGS_TABLE) $(CONFIG_FILE) $(XSECTION_ANALYSIS_FILE) $(OUT_PAYROLL_AVGS) $(OUT_STIPEND_AVGS)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--OUT_PAYROLL_AVGS=$(OUT_PAYROLL_AVGS) \
		--OUT_STIPEND_AVGS=$(OUT_STIPEND_AVGS) \
		--OUT_WAGE_TABLE=$(OUT_WAGE_TABLE)

$(OUT_WAGE_PERCENTILES): $(EARNINGS_PERCENTILES) $(CONFIG_FILE) $(PROJECT_CONFIG) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE) $(TASK_FNS)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--OUT_WAGE_PERCENTILES=$(OUT_WAGE_PERCENTILES)

$(OUT_AVAILABLE_HOURS): $(AVAILABLE_HOURS) $(CONFIG_FILE) $(PROJECT_CONFIG) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE) $(TASK_FNS)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--OUT_AVAILABLE_HOURS=$(OUT_AVAILABLE_HOURS)

$(OUT_MAIN_TABLE): $(RMD_FILE) $(OUT_WAGE_TABLE) $(OUT_WAGE_PERCENTILES) $(OUT_AVAILABLE_HOURS) $(OUT_TAKEUP_PLOT)
	$(R_script) -e "rmarkdown::render(input = '$(RMD_FILE)')"
	@cp $(OUT_DIR)/*up_to_date.tex $(PAPER_OUT_DIR)


.PHONY: all clean
.DEFAULT_GOAL=all

all: $(OUT_WAGE_GROWTH_PLOT) $(OUT_PAYROLL_AVGS) $(OUT_STIPEND_AVGS) $(OUT_WAGE_TABLE) $(OUT_WAGE_PERCENTILES) $(OUT_AVAILABLE_HOURS) $(OUT_MAIN_TABLE) $(OUT_TAKEUP_PLOT) $(OUT_TAKEUP_NOTED)

clean:
	rm -rf $(OUT_DIR)/*

